{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4e3adfbc",
   "metadata": {},
   "source": [
    "# Exploring Verified Auto Labeling\n",
    "\n",
    "Welcome to this hands-on workshop where we will learn how to load and explore datasets using FiftyOne. \n",
    "This notebook will guide you through programmatic interaction via the **FiftyOne SDK** and visualization using the **FiftyOne App**.\n",
    "\n",
    "![verified_autolabeling](https://cdn.voxel51.com/getting_started_manufacturing/notebook10/verified_autolabeling.webp)\n",
    "\n",
    "## Learning Objectives:\n",
    "- Load datasets into FiftyOne from different sources.\n",
    "- Understand the structure and metadata of datasets.\n",
    "- Calculate patches and gain insights using embeddings.\n",
    "- Understand what VAL - Verified Auto Labeling means and bring this to the analysis.\n",
    "\n",
    "In this example, we use dataset loading from directory and a Kaggle dataset.\n",
    "\n",
    "[Dataset Link](https://www.kaggle.com/datasets/mugheesahmad/sh17-dataset-for-ppe-detection)\n",
    "\n",
    "Download, extract and add the dataset in the root folder of this repo. I have called it \"ppe\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc7424a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import fiftyone as fo\n",
    "import shutil\n",
    "import os\n",
    "\n",
    "dataset_dir = \"ppe\"\n",
    "\n",
    "import fiftyone as fo\n",
    "\n",
    "import fiftyone as fo\n",
    "\n",
    "name = \"sh17-dataset_FO_patches\"\n",
    "data_path = \"ppe/images\"\n",
    "labels_path = \"ppe/labels\"\n",
    "classes = [\n",
    "    \"Person\",         # 0\n",
    "    \"Ear\",            # 1\n",
    "    \"Earmuffs\",       # 2\n",
    "    \"Face\",           # 3\n",
    "    \"Face-guard\",     # 4\n",
    "    \"Face-mask-medical\", # 5\n",
    "    \"Foot\",           # 6\n",
    "    \"Tools\",          # 7\n",
    "    \"Glasses\",        # 8\n",
    "    \"Gloves\",         # 9\n",
    "    \"Helmet\",         # 10\n",
    "    \"Hands\",          # 11\n",
    "    \"Head\",           # 12\n",
    "    \"Medical-suit\",   # 13\n",
    "    \"Shoes\",          # 14\n",
    "    \"Safety-suit\",    # 15\n",
    "    \"Safety-vest\"     # 16\n",
    "]\n",
    "\n",
    "# Import dataset by explicitly providing paths to the source media and labels\n",
    "dataset = fo.Dataset.from_dir(\n",
    "    dataset_type=fo.types.YOLOv4Dataset,\n",
    "    data_path=data_path,\n",
    "    labels_path=labels_path,\n",
    "    classes=classes,\n",
    "    name=name,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a456615c",
   "metadata": {},
   "outputs": [],
   "source": [
    "session = fo.launch_app(dataset, auto=False, port= 5152)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0222d728",
   "metadata": {},
   "source": [
    "## Calculate the embeddings of patches and visualize it\n",
    "\n",
    "Once you have the dataset in YOLO format FiftyOne will convert the dataset in FiftyOne format and you can calculate the embeddings selecting a model from the Build-In options que have in the FiftyOne Model Zoo. You can select multiple from multiple options, like CLIP, DINOv2, DINOv3, MobileNet, etc. \n",
    "\n",
    "[More about FiftyOne Model Zoo](https://docs.voxel51.com/model_zoo/index.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "738c618b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import fiftyone.zoo as foz\n",
    "# Specify the field containing the patches (e.g., detections)\n",
    "patches_field = \"ground_truth\"\n",
    "\n",
    "# Option 1: Use a pre-trained model from the FiftyOne Model Zoo\n",
    "model = foz.load_zoo_model(\"mobilenet-v2-imagenet-torch\")\n",
    "\n",
    "# Compute embeddings for the patches using the specified model\n",
    "dataset.compute_patch_embeddings(\n",
    "    patches_field=patches_field, \n",
    "    model=model, \n",
    "    embeddings_field=\"patches_embedding\",\n",
    "    num_workers=0\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfb58283",
   "metadata": {},
   "outputs": [],
   "source": [
    "import fiftyone.brain as fob\n",
    "\n",
    "fob.compute_visualization(\n",
    "    dataset,\n",
    "    patches_field=patches_field,            # your field\n",
    "    embeddings=\"patches_embedding\",         # patch embedding field\n",
    "    brain_key=\"dectection_patch_embeddings\",# name for this embedding run\n",
    "    num_workers=0\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7643b7ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset.persistent=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cd43971",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(dataset)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de08f43a",
   "metadata": {},
   "source": [
    "### Unlock VAL with FiftyOne Enterprise\n",
    "\n",
    "Experience the power of VAL (Visual Auto Labeling) with FiftyOne Enterprise! \n",
    "\n",
    "- Check the latest blog about VAL [BLOG VAL](https://voxel51.com/blog/zero-shot-auto-labeling-rivals-human-performance#1b3993879a50) to see VAL in action.\n",
    "\n",
    "- Want to learn more or get access? [Book a personalized demo](https://voxel51.com/sales) with our Customer Success Team—they're excited to show you how VAL can accelerate your workflows.\n",
    "- Ready to try FiftyOne? [Explore datasets in the cloud](https://try.fiftyone.ai/datasets)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d79de12",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Open in App (tabs)\n",
    "session = fo.launch_app(dataset, port=5151, auto=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "manu_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
